/*
 * createTime: 2020/12/14 20:12:29
 * description:
 */

const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const FORM = 'FORM';
const DELETE = 'DELETE';

const baseURL = 'https://zfx.xmonecode.com/api';

const API = {
    getBubble: (data) => request(GET, `/app/bubbleSetting/page?jsCode=${data}`),
};

module.exports = {
    API: API
};

function request(method, url, data) {
    return new Promise(function(resolve, reject) {
        let header = {
            'content-type': 'application/json',
        };
        wx.request({
            url: baseURL + url,
            method: method,
            data: method === POST ? JSON.stringify(data) : data,
            header: header,
            success: (res) => {
                //请求成功
                //判断状态码---errCode状态根据后端定义来判断
                const { code, data } = res.data;
                switch (code) {
                    case 1000:
                        resolve(data);
                        break;
                    default:
                        reject(res.data);
                }
            },
            fail: (err) => {
                //请求失败
                if (err.statusCode === 403) {
                    if (!btnLock) {
                        btnLock = true;
                        wx.showToast({
                            title: '登录信息已过期',
                            icon: 'none'
                        });
                        wx.removeStorageSync('token');

                        setTimeout(() => {
                            wx.reLaunch({
                                url: '/pages/index/index'
                            });
                        }, 2000);
                    }
                    return false;
                }
                reject(err);
            }
        })
    })
}